<?php

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\admin\model\Auth as AuthModel;
use app\admin\model\AuthGroup as AuthGroupModel;

class Auth extends Base
{
    //用户以及所在用户组关联展示列表首页渲染
    public function index(){

        return view();
    }
    //用户以及所在用户组数据请求接口
    public function user(){
        $model = new AuthModel;
        $page = input('get.page');
        $limit = input('get.limit');
        $group = $model->limit(($page - 1) * $limit, $limit)->select();

        //$data = AuthModel::find(2);
        $data = [];
        //$vv = [];
        foreach($group as $k=> $v){
            $vv = $v->toArray();
            $g_str = "";
            $g_rules = "";
            foreach ($v->authGroup as $c){
                $g_str .= $c->title .",";
                $g_rules .= $c->rules .",";
            }
            $vv['title'] = $g_str;
            $array = explode(',',trim($g_rules,','));
            $new = array_unique($array);
            $str = implode($new,',');
            $vv['rules'] = $str;

            $data[] = $vv;
        }
        $count = count($data);
        //dump($data);die;
        return json(['code' => 0, 'msg' => "", 'count' => $count, 'data' => $data]);

    }
    //添加用户以及所在用户组页面渲染
    public function add(){
        $groups = Db::name('AuthGroup')->select();
        //dump($groups);die;
        $this->assign('groups',$groups);
        return view();
    }
    //用户以及所在用户组关联添加数据处理
    public function add_do(){
        $data = input('post.');
        //$data['group'] = implode(',', array_keys($data['group']));
        //dump($data);die;
        $model = new AuthModel;
        $data["password"] = md5($data["password"]);
        $res = $model->allowField(true)->save($data);
        if($res){
            if(!empty($data["group"])){
                $user = $model->where('user_name',$data["user_name"])->find();
                foreach($data["group"] as $key=>$val){
                    $user->authGroup()->attach($key);
                }
                return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
            }else{
                return json(['code' => 0, 'msg' => '修改失败']);
            }
        }else{
            return json(['code' => 1, 'msg' => '修改失败']);
        }
    }
    //用户以及所在用户组编辑页面渲染
    public function Update(){
        $id = input('get.');
        $model = new AuthModel();
        $msg = $model->find($id);
        $data = $msg->toArray();
        $user=[];
        foreach($msg->authGroup as $key=>$v){
            $user[]= $v->id;
        }
        $data['title'] = $user;
        $groups = Db::name('AuthGroup')->select();
        //dump($groups);die;
        $this->assign('groups',$groups);
        //dump($data);
        $this->assign('data',$data);
        //dump($data);die;
        return view();
    }
    //用户以及所在用户组编辑数据处理
    public function doAuthUpdate(){
        $data = input('post.');
        $model = new AuthModel();
        $id = $data["id"];
        $res = $model->isUpdate(true)->allowField(true)->save($data);
        $user = $model->find($id);
        if($res && !empty($data["group"])){
            $k=[];
            //dump($user->authGroup());die;
            foreach($user->authGroup as $key=>$v){
                $k[] = $v->id;
            }
            //dump($k);die;
            $user->authGroup()->detach($k);
            $user = $model->where('user_name', $data["user_name"])->find();
            foreach ($data["group"] as $key => $val) {
                $user->authGroup()->attach($key);
            }
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }
        return json(['code' => 0, 'msg' => '修改失败']);
    }


    //用户以及关联数据删除
    public function delUser()
    {
        //dump(input('post.id'));die;
        $id = input('post.id');
        //dump($id);die;
        $model = new AuthModel();
        $user = $model->find($id);
        //dump($user);
        $k = [];
        //dump($user->authGroup());die;
        foreach ($user->authGroup as $key => $v) {
            $k[] = $v->id;
        }
        //dump($k);die;
        if(!empty($k)) {
            $user->authGroup()->detach($k);
        }
        //dump($group);die;

        $res = $model->where('id',$id)->delete();
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 1, 'msg' => '删除失败']);
        }
    }
}